// firstPackages/pages/videoPlay/videoPlay.js
import { queryVideoDetail } from "../../service/house_service"
import { loginWxAction } from "../../service/config"
import { bindUserRelation } from "../../service/user_service"
import {
  articleDetail
} from "../../service/jb_article_service"
import {
  BIND_USER_ID
} from "../../service/base_service"
import {
  VIDEO_COVER,
} from "../../service/base_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoCoverUrl: VIDEO_COVER,
    videoUrl: "",
    id: "",
    isShare: false,
    userId: "",
    bindUserId: "",
    shareName: "",
    knoData: {
      title: "",
      synopsis: "",
      imageInput: '',
    },

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })

    if (options.isShare == 1) {
      this.setData({
        isShareEnter: true,
      })
      if (options.userId) {
        wx.setStorageSync(BIND_USER_ID, options.userId);
      }
      this.userWxLogin();
    } else {
      this.queryKnoDetail();
    }


    // 绑定分享参数
    wx.onCopyUrl(() => {
      return { query: `userId=${app.globalData.userId}&isShare=1` }
    })
  },
  onHide() {
    wx.offCopyUrl()
  },
  userWxLogin() {
    loginWxAction().then((res => {
      console.log("JJLog:登录返回:" + JSON.stringify(res));
      let data = res.data;
      if (res.code == 200) {
        app.globalData.nickName = data.nickname || "";
        app.globalData.avatar = data.avatar || "";
        app.globalData.phone = data.phone || "";
        app.globalData.isLogin = data.isLogin;
        app.globalData.userId = data.uid;
        this.queryKnoDetail();
      }
    })).catch(err => {
      console.error("JJLog:userWxLogin", err);

    })
  },
  queryKnoDetail() {

    wx.showLoading();

    articleDetail(this.data.id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          knoData: res.data,

        })
      }
    }).catch(err => {
      if (this.data.loading) {
        wx.hideLoading();
      }
    })
  },
  //跳转到商品详情
  pushToProduct(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/shopMallPackage/pages/newStoreDetail/newStoreDetail?houseNumber=' + item.id + "&type=normal",
    })
  },

  //用户登陆授权等:
  // userWxLogin() {
  //   loginWxAction().then((res => {
  //     console.log("JJLog:登录返回:" + JSON.stringify(res));
  //     let data = res.data.data;

  //     if (res.code == 200) {

  //       app.globalData.nickName = data.nickName;
  //       app.globalData.avatar = data.avatar;
  //       app.globalData.name = data.name || "";
  //       app.globalData.phone = data.phone || "";
  //       app.globalData.isLogin = data.isLogin;
  //       app.globalData.userId = data.id;
  //       app.globalData.qrCode = data.qrCode || "";
  //       app.globalData.isAuthUserInfo = data.isAuthUserInfo;
  //       app.globalData.weChat = data.weChat || "";
  //       app.globalData.roleName = data.roleName || "";
  //       app.globalData.companyName = data.companyName || "";
  //       app.globalData.companyNumber = data.companyNumber || "";
  //       app.globalData.isBindMp = data.isBindMp;
  //       app.globalData.roleType = data.roleType || 1


  //       //绑定挂级关系:
  //       if (this.data.bindUserId) {
  //         this.bindUserRelation();
  //       }



  //     }
  //   })).catch(err => {
  //     console.error("JJLog:userWxLogin", err);

  //   })
  // },


  //视频播放失败回调;
  videoErrorCallback(e) {
    console.log("JJLog:视频播放失败:" + JSON.stringify(e));
  },
  //缓存中...
  videoWaiting(e) {
    console.log("JJLog:缓存zhong：" + JSON.stringify(e));
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },





  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },




  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function (res) {
  //   let name = this.data.shareName || app.globalData.commonShareTitle
  //   return {
  //     title: name,
  //     imageUrl: this.data.videoUrl + "?vframe/jpg/offset/1",
  //     path: "/pages/videoPlay/videoPlay?isShare=1" + "&id=" + this.data.id + "&userId=" + this.data.userId,
  //     success: (res) => {
  //       console.log("转发成功", res);
  //     }, fail: (res) => {
  //       console.log("转发失败", res);
  //     }
  //   }
  // },
  //朋友圈分享:
  // onShareTimeline: function () {
  //   let name = this.data.shareName || app.globalData.commonShareTitle
  //   return {
  //     title: name,
  //     query: "isShare=1" + "&id=" + this.data.id + "&userId=" + this.data.userId,
  //     imageUrl: this.data.videoUrl + "?vframe/jpg/offset/1"
  //   }
  // },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = app.globalData.userId || ""
    let path = "/pages/videoPlay/videoPlay?isShare=1" + "&userId=" + userId + "&id=" + this.data.knoData.id
    return {
      title: this.data.knoData.title,
      path: path
    }
  }

})